package com.jinmdz.fmis.dao.admin.dao.provider;

import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 物品服务Provider
 *
 * @author LiCongLu
 * @date 2020-03-26 13:36
 */
public class GoodsServiceProvider extends BaseProvider {

    /**
     * 按照类型查询物品服务
     *
     * @param typeId  类型主键
     * @param keyword 关键字
     * @return
     * @author LiCongLu
     * @date 2020-03-26 13:36
     */
    public String listGoodsServiceListByTypeId(@Param("typeId") Integer typeId, @Param("keyword") String keyword) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_item.id,service_type_id,item_code,item_name,item_name_abbr,item_price,item_number,item_unit ");
                builder.append(" ,item_describe,as_goods,as_package,as_fix_price,service_item.disable_state,service_item.sort,service_item.remark,service_item.version ");
                builder.append(" ,service_type.type_name as service_type_name");
                SELECT(builder.toString());
                FROM(" service_item ");
                JOIN(" service_type on service_type.id = service_type_id ");
                WHERE(" service_item.deleted = 0 and service_type.deleted = 0 ");
                WHERE(" service_type_id = #{typeId}");

                // 关键字检索
                if (valid(keyword)) {
                    WHERE(" ( item_name like concat('%',#{keyword},'%') or item_name_abbr like concat('%',#{keyword},'%') or item_price like concat('%',#{keyword},'%') ) ");
                }

                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }

    /**
     * 获取非物品包集合
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-03-26 14:09
     */
    public String listGoodsServiceListNoPackage() {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_item.id,service_type_id,item_code,item_name,item_name_abbr,item_price,item_number,item_unit ");
                builder.append(" ,item_describe,as_goods,as_package,as_fix_price,service_item.disable_state,service_item.sort,service_item.remark,service_item.version ");
                builder.append(" ,service_type.type_name as service_type_name");
                SELECT(builder.toString());
                FROM(" service_item ");
                JOIN(" service_type on service_type.id = service_type_id ");
                WHERE(" service_item.deleted = 0 and service_type.deleted = 0 ");
                WHERE(" service_item.disable_state = 0 and service_item.as_package = 0 ");
                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }

    /**
     * 按照主键获取物品服务信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-03-26 15:24
     */
    public String getGoodsServiceEditById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_item.id,service_type_id,item_code,item_name,item_name_abbr,item_price,item_number,item_unit ");
                builder.append(" ,item_describe,as_goods,as_package,as_fix_price,service_item.disable_state,service_item.sort,service_item.remark,service_item.version ");
                builder.append(" ,service_type.type_name as service_type_name");
                SELECT(builder.toString());
                FROM(" service_item ");
                JOIN(" service_type on service_type.id = service_type_id ");
                WHERE(" service_item.deleted = 0 and service_type.deleted = 0 ");
                WHERE(" service_item.id = #{id} ");
                ORDER_BY(" service_item.id limit 1 ");
            }
        }.toString();
    }

    /**
     * 查询所有有效物品、服务信息
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-03-27 09:49
     */
    public String listGoodsServiceListAsValid() {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" service_item.id,service_type_id,item_code,item_name,item_name_abbr,item_price,item_number,item_unit ");
                builder.append(" ,item_describe,as_goods,as_package,as_fix_price,service_item.disable_state,service_item.sort,service_item.remark,service_item.version ");
                builder.append(" ,service_type.type_name as service_type_name");
                SELECT(builder.toString());
                FROM(" service_item ");
                JOIN(" service_type on service_type.id = service_type_id ");
                WHERE(" service_item.deleted = 0 and service_type.deleted = 0 ");
                WHERE(" service_item.disable_state = 0 ");
                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }
}
